package com.emmmya.ocs.config.jpa;

import com.emmmya.ocs.config.security.jwt.JwtConfig;
import com.emmmya.ocs.modules.system.entity.User;
import com.emmmya.ocs.modules.system.utils.UserUtils;
import com.emmmya.ocs.config.redis.RedisManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;

import java.util.Optional;

/**
 * 审计记录创建或修改用户
 */
@Configuration
@Slf4j
public class UserAuditor implements AuditorAware<String> {
    @Autowired
    JwtConfig jwtConfig;
    @Autowired
    RedisManager redisManager;
    @Autowired
    UserUtils userUtils;
    @Override
    public Optional<String> getCurrentAuditor() {
        try {
            Subject subject = SecurityUtils.getSubject();
            User currentUser = (User) subject.getPrincipal();
            String username = "";
            if(currentUser !=null){
                username = currentUser.getUsername();
            }

            return Optional.ofNullable(username);
        }catch (Exception e){
            return Optional.empty();
        }
    }
}
